package com.rhino.xchat

import android.app.Application
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.ProcessUtils
import com.google.gson.reflect.TypeToken
import com.rhino.base.BaseApplication
import com.rhino.base.config.AppEnv
import com.rhino.base.debugger.DebuggerHelper
import com.rhino.base.http.HttpService
import com.rhino.base.utils.TimeUtils
import com.rhino.magiccubecircle.http.TokenInterceptor
import com.rhino.xchat.http.HeaderInterceptor

/**
 * @author LuoLin
 * @since Create on 2025/3/31.
 */
class App : BaseApplication() {

    override fun initAppInfo() {
        appInfo.isDebug = BuildConfig.DEBUG
        appInfo.isInnerTest = BuildConfig.IS_INNER_TEST
        appInfo.buildTime = TimeUtils.getTimeStampByTimeString(
            BuildConfig.BUILD_TIME,
            "yyyy_MM_dd_HH_mm_ss"
        )
        val appEnvJson = BuildConfig.APP_ENV_LIST.replace("!", "\"")
        appInfo.appEnvMap =
            GsonUtils.fromJson(appEnvJson, object : TypeToken<Map<String, AppEnv>>() {}.type)
        appInfo.appEnvMap[AppEnv.getEnvName()]?.let {
            appInfo.appEnv = it
        }
    }

    override fun onCreate() {
        super.onCreate()
        // 禁止三方服务重复初始化
        if (applicationContext.packageName != ProcessUtils.getCurrentProcessName()) {
            return
        }
        // Http请求拦截器
        HttpService.interceptorList.apply {
            add(HeaderInterceptor())
            add(TokenInterceptor())
        }
        // 调试用
        if (appInfo.isInnerTest) {
            // 初始化调试工具
            DebuggerHelper.init(this)
        }
        // TODO ll app是否可用
//        val appAvailableTime = 180L * TimeUtils.MILLISECONDS_PER_DAY
//        val appUseTimestamp = System.currentTimeMillis() - appInfo.buildTime
//        LogUtils.w("buildTime: ${appInfo.buildTime}, appAvailableTime: $appAvailableTime, appUseTimestamp: $appUseTimestamp")
//        if (appUseTimestamp > appAvailableTime) {
//            LogUtils.e("unable app!!!")
//            throw RuntimeException("unable app!!!")
//        }
    }

    companion object {
        fun init(context: Application) {

        }
    }

}